<?php
namespace app\admin\controller;

use app\admin\library\AdminController;
use cubox\Cubox;
use cubox\Data;

class Driver extends AdminController{
    
    
    public function onList(){
        $type = $this->post('type',$this->check()->type('string')->required());
        $app = $this->post('app',$this->check()->type('string'));

        if (!in_array($type,['database','cache'])){
            throw new \Exception('参数错误');
        }

        if ($app){
            $data = new Data(ROOT.DS.CONFIG.DS.$app.DS.$type.'.php');
        }else{
            $data = Cubox::config($type)->toArray();
        }

        $default = $data['default']??'';
        unset($data['default']);

        $list = [];
        foreach($data as $name => $config){
            $driver = $config['driver'];

            if (isset($config['password'])) unset($config['password']);

            $item  = [
                'name' => $name,
                'driver' => ucfirst($driver),
                'default' =>  $default == $name,
                'config'=> $config,
            ];
            
            try{
                $className =  '\\cubox\\'.$type.'\\'.ucfirst($driver);
                $item['title'] = $className::frontTitle($config);
            }catch(\Exception $e){
                $item['title'] =  '驱动错误';
            }
            $list[] = $item;
        }

        $param = [];
        $fileList = scandir(ROOT.DS.CUBOX.DS.$type);
        for($i=0;$i<count($fileList);$i++){
            if(strpos($fileList[$i], '.php') !== false){
                $className = str_replace('.php', '', $fileList[$i]);
                $class = '\\cubox\\'.$type.'\\'.$className;
                $param[ucfirst($className)] = $class::configParams();
            }
        }

        return ['list' => $list, 'param' => $param];
    }

    public function onSave(){
        $type = $this->post('type',$this->check()->type('string')->required());
        $app = $this->post('app',$this->check()->type('string'));
        $name = $this->post('name',$this->check()->type('string')->required());
        $edit = $this->post('edit',$this->check()->type('bool')->default(false));
        $config = $this->post('config',$this->check()->type('array')->required());

        if (!in_array($type,['database','cache'])){
            throw new \Exception('参数错误');
        }


        if ($app){
            $data = new Data(ROOT.DS.CONFIG.DS.$app.DS.$type.'.php');
        }else{
            $data = Cubox::config($type);
        }

        if ($edit){
            if ($data->exists($name)) $data->$name =  $config;
            else{
                throw new \Exception('配置不存在');
            }
        }else{
            if ($data->exists($name)) {
                throw new \Exception('配置名称已被使用');
            }else{
                $data->$name =  $config;
            }
        }

        $data->save();
        return true;
    }

    public function onDel(){
        $type = $this->post('type',$this->check()->type('string')->required());
        $app = $this->post('app',$this->check()->type('string'));
        $name = $this->post('name',$this->check()->type('string')->required());
        if (!in_array($type,['database','cache'])){
            throw new \Exception('参数错误');
        }
        if ($app){
            $data = new Data(ROOT.DS.CONFIG.DS.$app.DS.$type.'.php');
        }else{
            $data = Cubox::config($type);
        }

        if ($name == $data->default){
            throw new \Exception('默认配置不能删除');
        }

        if ($data->exists($name)){
            unset($data->$name);
            $data->save();
            return true;
        }else{
            throw new \Exception('配置不存在');
        }
    }
}